#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <db.h>
#include "uniondef.h"

#define SHORT_OPTION_STRING ""
struct option long_options[] =
{
    /* options */
    {"patternID", required_argument, 0, 'i'},
    {0, 0, 0, 0}
};

int parseArgs (int argc, const char * argv[], char * idString)
{
    int c;

    while (1)
    {
        int option_index = 0;

        c = getopt_long(argc, argv, SHORT_OPTION_STRING, long_options, &option_index);

        /* detect the end of the options */
        if (c == -1)
        {
            break;
        }
        switch (c)
        {
            case 0:
            case '?':
                break;

            case 'i':
                strcpy(idString, optarg);
                break;
            default:
                exit(1);
        }
    }
}


int main (int argc, const char * argv[]) 
{
    DB *dbp;           /* DB structure handle */
    DBT key, value;
    unsigned int flags;/* database open flags */
    int ret;           /* function reture value */
    flags = DB_RDONLY;
    char patternID[256];
    AccessPattern * ptn;

    memset(patternID, 0, 256);
    parseArgs(argc, argv, (char *)patternID);

    /* open the database */
    ret = db_create(&dbp, NULL, 0);
    if (ret != 0) {
        /* Error handling goes here */
        exit(1);
    }

    /* open DB */
    ret = dbp->open(dbp, NULL, "/mnt/common/yyin/PDLA/patterndb.db", NULL, DB_HASH, flags, 0);
    //ret = dbp->open(dbp, NULL, "my.db", NULL, DB_HASH, flags, 0);
    if (ret != 0) {
        /* Error handling */
        fprintf(stderr, "Openning DB failed ... \n");
        if (dbp != NULL) {
            dbp->close(dbp, 0);
        }
        exit(1);
    }

    memset(&key, 0, sizeof(DBT));
    memset(&value, 0, sizeof(DBT));

    key.data = patternID;
    key.size = strlen(patternID)+1;

    ret = dbp->get(dbp, NULL, &key, &value, 0);
    if (ret != 0) {
        /* Error handling */
        fprintf(stderr, "dbp-get: %s\n", db_strerror(ret));
        if (dbp != NULL) {
            dbp->close(dbp, 0);
        }
        exit(1);
    }

    if (value.size == 0) {
        fprintf(stderr, "Error: Read %d bytes of data\n", value.size);
        if (dbp != NULL) {
            dbp->close(dbp, 0);
        }
        exit(1);
    }

    ptn = (AccessPattern *) value.data;

    // display the pattern information.
    fprintf(stdout, "======== Pattern Information ======== \n");
    fprintf(stdout, "\t        Pattern Type: %d \n", ptn->patternType);
    fprintf(stdout, "\t    Initial Position: %d \n", ptn->initPos);
    fprintf(stdout, "\t        Initial Size: %d \n", ptn->initSize);
    fprintf(stdout, "\t Number of Repetions: %d \n", ptn->numRep);
    fprintf(stdout, "\t         Stride Size: %d \n", ptn->strideSize);
    fprintf(stdout, "\t           Operation: %d \n", ptn->operation);
    fprintf(stdout, "\t            Filename: %s \n", ptn->filename);

    /* close DB */
    if (dbp != NULL) {
        dbp->close(dbp, 0);
    }

    return 0;
}

///:~

